package cn.zhaozhiqi.article.controller;

import cn.zhaozhiqi.article.vo.ArticleTypeTreeVO;
import cn.zhaozhiqi.exception.BussinessException;
import cn.zhaozhiqi.article.domain.ArticleType;
import cn.zhaozhiqi.article.query.ArticleTypeQuery;
import cn.zhaozhiqi.article.service.IArticleTypeService;
import cn.zhaozhiqi.util.R;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/articleType")
@Slf4j
public class ArticleTypeController {

    @Autowired
    private IArticleTypeService articleTypeService;
    //1.查询一条记录
    @GetMapping("/{id}")
    public R<ArticleType> getByID(@PathVariable("id")Long id){
        try {
            ArticleType articleType = articleTypeService.getByID(id);
            return R.ok(articleType);
        } catch (BussinessException e) {
            log.info("查询用户操作异常：{}",e.getMessage());
            return R.fail("查询失败");
        }catch (Exception e) {
            log.info("查询用户系统异常：{}",e.getMessage());
            return R.fail("系统异常，查询失败");
        }
    }
    //2.1查询所有记录
    @GetMapping()
    public R<List<ArticleType>> getAll(){
        try {
            List<ArticleType> list = articleTypeService.getAll();
            return R.ok(list);
        } catch (BussinessException e) {
            log.info("查询用户操作异常：{}",e.getMessage());
            return R.fail("查询失败");
        }catch (Exception e) {
            log.info("查询用户系统异常：{}",e.getMessage());
            return R.fail("系统异常，查询失败");
        }
    }
    //2.新增一条记录
    @PostMapping("/add")
    public R<Void> addData(@RequestBody ArticleType articleType){
        try {
            articleTypeService.addData(articleType);
            return R.ok("新增成功");
        } catch (BussinessException e) {
            log.info("新增用户操作异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }catch (Exception e) {
            log.info("新增用户系统异常：{}",e.getMessage());
            return R.fail();
        }
    }
    //3.修改一条记录
    @PostMapping("/update")
    public R<Void> updateByID(@RequestBody ArticleType articleType){
        try {
            articleTypeService.updateByID(articleType);
            return R.ok("修改成功");
        } catch (BussinessException e) {
            log.info("修改用户操作异常：{}",e.getMessage());
            return R.fail("修改失败");
        }catch (Exception e) {
            log.info("修改用户系统异常：{}",e.getMessage());
            return R.fail("系统异常，修改失败");
        }
    }
    //4.删除一条记录
    @DeleteMapping("/delete/{id}")
    public R<Void> deleteByID(@PathVariable("id")Long id){
        try {
            articleTypeService.deleteByID(id);
            return R.ok("删除成功");
        } catch (BussinessException e) {
            log.info("删除用户操作异常：{}",e.getMessage());
            return R.fail("删除失败");
        }catch (Exception e) {
            log.info("删除用户系统异常：{}",e.getMessage());
            return R.fail("系统异常，删除失败");
        }
    }
    //5.查询分页
    @PostMapping("/page")
    public R<PageInfo<ArticleType>> articleTypeLimit(@RequestBody ArticleTypeQuery query){
        try {
            PageInfo<ArticleType> pageInfo = articleTypeService.articleTypeLimit(query);
            return R.ok(pageInfo);
        } catch (BussinessException e) {
            e.printStackTrace();
            log.info("分页操作异常：{}",e.getMessage());
            return R.fail("删除失败");
        }catch (Exception e) {
            e.printStackTrace();
            log.info("分页系统异常：{}",e.getMessage());
            return R.fail("系统异常，分页系统异常");
        }
    }

    //6.删除全部
    @PostMapping("/batchdelete")
    public R<Void> batchdelete(@RequestBody List<Long> ids){
        try {
            articleTypeService.batchdelete(ids);
            return R.ok("删除成功");
        } catch (BussinessException e) {
            log.info("删除用户操作异常：{}",e.getMessage());
            e.printStackTrace();
            return R.fail("删除失败");
        }catch (Exception e) {
            log.info("删除用户系统异常：{}",e.getMessage());
            return R.fail("系统异常，删除失败");
        }
    }

    //7.新增无限极树
    @PostMapping("/tree")
    /*无限极树的接口，前端发出请求，是*/
    public R<List<ArticleTypeTreeVO>> tree(){
        try {
            List<ArticleTypeTreeVO> tree = articleTypeService.tree();
            return R.ok(tree);
        } catch (BussinessException e) {
            log.info("删除用户操作异常：{}",e.getMessage());
            e.printStackTrace();
            return R.fail("删除失败");
        }catch (Exception e) {
            log.info("删除用户系统异常：{}",e.getMessage());
            return R.fail("系统异常，删除失败");
        }
    }
}
